package cn.edu.ctbu.datajpa01.service.impl;


import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.domain.repository.StudentRepository;

import cn.edu.ctbu.datajpa01.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentImpIService implements StudentService {

    @Autowired
    private StudentRepository studentRepository;

    public List<Student> findAll(){

        return studentRepository.findAll();
    }

    /**
     * 只分页的查询
     * @param pageable
     * @return
     */
    public Page<Student> findAll(Pageable pageable){

        return studentRepository.findAll(pageable);

    }

    public Page<Student> findAll(Example<Student> student,Pageable pageable){

        return studentRepository.findAll(student,pageable);

    }

    //按id进行查询 @param id 主键，整数
    //@return
    public Student findById(Integer id){

        return studentRepository.findById(id).orElse( null);
    }

    //按名字进行查询 like
    // @param id 主键，整数
    //@return
    public List<Student> findByname(String name){


        return studentRepository.findBynameLike(name);
    }

    public Student getById(Integer id){

        Student student=studentRepository.findById(id).orElse(null);
        return student;
    }

    public Student insert(Student student){

        studentRepository.save(student);
        return student;
    }

    public Student update(Student student){

        studentRepository.save(student);
        return student;
    }

    public void delete(Student student){

        studentRepository.delete(student);
    }

    public void delete(Integer id){

        Student student=new Student();
        student.setId(id);
        studentRepository.delete(student);
    }


    public Student findByName(String name){

        return studentRepository.findByName(name);
    }


    public List<Student> findByNoAndPassword(String no, String password) {

        return studentRepository.findByNoAndPassword(no,password);
   }

}
